@echo off
chcp 65001 >nul
echo ====================================
echo API问题完全诊断
echo ====================================
echo.

echo 🔍 步骤1: 检查后端服务状态
echo ------------------------------------
curl -s -w "状态码: %%{http_code}\n响应时间: %%{time_total}s\n" http://localhost:8081/api/health
echo.

echo 🔍 步骤2: 检查CORS设置
echo ------------------------------------
curl -s -X OPTIONS -H "Origin: http://localhost:5173" -H "Access-Control-Request-Method: GET" -H "Access-Control-Request-Headers: Content-Type" -w "状态码: %%{http_code}\n" http://localhost:8081/api/drugs
echo.

echo 🔍 步骤3: 检查药品API（详细模式）
echo ------------------------------------
echo 调用: GET /api/drugs
curl -s -w "\n状态码: %%{http_code}\n响应时间: %%{time_total}s\n响应大小: %%{size_download} bytes\n内容类型: %%{content_type}\n" http://localhost:8081/api/drugs
echo.
echo.

echo 🔍 步骤4: 检查库存API（详细模式）
echo ------------------------------------
echo 调用: GET /api/inventories
curl -s -w "\n状态码: %%{http_code}\n响应时间: %%{time_total}s\n响应大小: %%{size_download} bytes\n内容类型: %%{content_type}\n" http://localhost:8081/api/inventories
echo.
echo.

echo 🔍 步骤5: 检查认证API
echo ------------------------------------
echo 调用: POST /api/auth/login
curl -s -X POST -H "Content-Type: application/json" -d "{\"username\":\"testuser2024\",\"password\":\"12345\"}" -w "\n状态码: %%{http_code}\n响应时间: %%{time_total}s\n" http://localhost:8081/api/auth/login
echo.
echo.

echo 🔍 步骤6: 检查数据库连接
echo ------------------------------------
mysql -u root -pROOT -e "USE medicalstore; SELECT 'Database Status:' as check_result, COUNT(*) as drug_count FROM drugs; SELECT 'User Count:' as check_result, COUNT(*) as user_count FROM users;" 2>nul
if %errorlevel% neq 0 (
    echo ❌ 数据库连接失败
) else (
    echo ✅ 数据库连接正常
)
echo.

echo 🔍 步骤7: 检查Spring Boot启动日志
echo ------------------------------------
echo 检查后端控制台输出，查看：
echo - 是否有端口冲突
echo - 是否有数据库连接错误
echo - 是否有Spring Security配置问题
echo - 是否有Controller路径映射问题
echo.

echo 🔍 步骤8: 前端调试建议
echo ------------------------------------
echo 1. 打开浏览器开发者工具
echo 2. 访问: http://localhost:5173/login
echo 3. 查看Network标签页的请求详情
echo 4. 检查Console中的错误信息
echo 5. 确认请求URL是否正确
echo.

echo ====================================
echo 常见问题解决方案
echo ====================================
echo.

echo 🔧 403 Forbidden 错误:
echo   - 检查SecurityConfig是否正确配置
echo   - 确认接口路径是否在permitAll列表中
echo   - 验证CORS配置是否正确
echo.

echo 🔧 JSON解析错误:
echo   - 检查后端是否返回HTML错误页面
echo   - 确认Content-Type是否为application/json
echo   - 验证响应内容是否为有效JSON
echo.

echo 🔧 网络连接问题:
echo   - 确认后端服务在8081端口运行
echo   - 检查防火墙设置
echo   - 验证前端API基础URL配置
echo.

echo ====================================
echo 诊断完成
echo ====================================

pause 